<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateChannelUserItemAPIRequest;
use App\Http\Requests\API\UpdateChannelUserItemAPIRequest;
use App\Models\ChannelUserItem;
use App\Repositories\ChannelUserItemRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class ChannelUserItemController
 * @package App\Http\Controllers\API
 */

class ChannelUserItemAPIController extends AppBaseController
{
    /** @var  ChannelUserItemRepository */
    private $channelUserItemRepository;

    public function __construct(ChannelUserItemRepository $channelUserItemRepo)
    {
        $this->channelUserItemRepository = $channelUserItemRepo;
    }

    /**
     * @param Request $request
     * @return Response
     *
     * @SWG\Get(
     *      path="/channelUserItems",
     *      summary="Get a listing of the ChannelUserItems.",
     *      tags={"ChannelUserItem"},
     *      description="Get all ChannelUserItems",
     *      produces={"application/json"},
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  @SWG\Items(ref="#/definitions/ChannelUserItem")
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $this->channelUserItemRepository->pushCriteria(new RequestCriteria($request));
        $this->channelUserItemRepository->pushCriteria(new LimitOffsetCriteria($request));
        $channelUserItems = $this->channelUserItemRepository->all();

        return $this->sendResponse($channelUserItems->toArray(), trans('flash.api.retrieve_success', ['model' => 'Channel User Items']));
    }

    /**
     * @param CreateChannelUserItemAPIRequest $request
     * @return Response
     *
     * @SWG\Post(
     *      path="/channelUserItems",
     *      summary="Store a newly created ChannelUserItem in storage",
     *      tags={"ChannelUserItem"},
     *      description="Store ChannelUserItem",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="ChannelUserItem that should be stored",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/ChannelUserItem")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/ChannelUserItem"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function store(CreateChannelUserItemAPIRequest $request)
    {
        $input = $request->all();

        $channelUserItems = $this->channelUserItemRepository->create($input);

        return $this->sendResponse($channelUserItems->toArray(), trans('flash.api.save_success', ['model' => 'Channel User Item']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Get(
     *      path="/channelUserItems/{id}",
     *      summary="Display the specified ChannelUserItem",
     *      tags={"ChannelUserItem"},
     *      description="Get ChannelUserItem",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of ChannelUserItem",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/ChannelUserItem"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function show($id)
    {
        /** @var ChannelUserItem $channelUserItem */
        $channelUserItem = $this->channelUserItemRepository->findWithoutFail($id);

        if (empty($channelUserItem)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'Channel User Item']), 404);
        }

        return $this->sendResponse($channelUserItem->toArray(), trans('flash.api.retrieve_success', ['model' => 'Channel User Item']));
    }

    /**
     * @param int $id
     * @param UpdateChannelUserItemAPIRequest $request
     * @return Response
     *
     * @SWG\Put(
     *      path="/channelUserItems/{id}",
     *      summary="Update the specified ChannelUserItem in storage",
     *      tags={"ChannelUserItem"},
     *      description="Update ChannelUserItem",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of ChannelUserItem",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="ChannelUserItem that should be updated",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/ChannelUserItem")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/ChannelUserItem"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function update($id, UpdateChannelUserItemAPIRequest $request)
    {
        $input = $request->all();

        /** @var ChannelUserItem $channelUserItem */
        $channelUserItem = $this->channelUserItemRepository->findWithoutFail($id);

        if (empty($channelUserItem)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'Channel User Item']), 404);
        }

        $channelUserItem = $this->channelUserItemRepository->update($input, $id);

        return $this->sendResponse($channelUserItem->toArray(), trans('flash.api.update_success', ['model' => 'ChannelUserItem']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/channelUserItems/{id}",
     *      summary="Remove the specified ChannelUserItem from storage",
     *      tags={"ChannelUserItem"},
     *      description="Delete ChannelUserItem",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of ChannelUserItem",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="string"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function destroy($id)
    {
        /** @var ChannelUserItem $channelUserItem */
        $channelUserItem = $this->channelUserItemRepository->findWithoutFail($id);

        if (empty($channelUserItem)) {
            return $this->sendError('Channel User Item not found', 404);
        }

        $channelUserItem->delete();

        return $this->sendResponse($id, trans('flash.api.delete_success', ['model' => 'Channel User Item']));
    }
}
